// SitePersist.cpp: implementation of the SitePersist class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "SitePersist.h"
#include "Site.h"
#include "DatabaseBase.h"
#include "SiteTable.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

using namespace PDMS::PDMSDBIF;
using namespace PDMS::PDMSDomain;

BEGIN_REGISTER_MAP( SitePersist, PersistRegBase )
REGISTER( UE_RUNTIME_CLASS( Site ), GET_REGISTER( PersistRegBase, VersionOne ) )
END_REGISTER_MAP();

SitePersist::SitePersist()
{

}

SitePersist::~SitePersist()
{

}

UESoft::ErrorStatus SitePersist::Retrieve( Persistable*& pObj )
{
	UESoft::ErrorStatus es = ReadPaternity( pObj );
	if ( es != UESoft::eOk )
	{
		return es;
	}

	if ( NULL == pObj )
	{
		return UESoft::eNullEntityPointer;
	}
	if ( !pObj->IsKindOf( UE_RUNTIME_CLASS( Site ) ) )
	{
		return UESoft::eUnKnownObjectPtrType;
	}
	Site* pSite = static_cast<Site*>( pObj );
	
	DatabaseBase* pBase = NULL;
	pBase = GetDatabase();
	ASSERT( pBase != NULL );
	DatabaseElement* pDbE = NULL;
	pDbE = pBase->GetDatabaseElement( _T("SiteData") );
	ASSERT( pDbE != NULL );
	
	StdString strCondition;
	strCondition.Format( _T("ID = %ld"), pObj->Id().ObjectID() );
	
	SiteTable* pST = static_cast<SiteTable*>( pDbE );
	es = pST->Open( strCondition );
	ASSERT( es == UESoft::eOk || es == UESoft::ePersisterFacadeIsOpen );

	pSite->OwnerId( UeObjectId( pSite->Id().EnviromentID(), pST->GetParentID() ) );
	pSite->Description( pST->GetDescription() );
	pSite->SetPurpose( pST->GetPurpose() );
	pSite->SetFunction( pST->GetFunction() );
	pSite->SetNumber( pST->GetNumber() );
	pSite->SetArea( pST->GetArea() );
	pSite->SetModule( pST->GetModule() );
	pSite->SetRlstored( pST->GetRlstored() );

	return UESoft::eOk;
}

UESoft::ErrorStatus SitePersist::Create( Persistable* pObj )
{
	UESoft::ErrorStatus es = CreatePaternity( pObj );
	if ( es != UESoft::eOk )
	{
		return es;
	}

	if ( NULL == pObj )
	{
		return UESoft::eNullEntityPointer;
	}
	if ( !pObj->IsKindOf( UE_RUNTIME_CLASS( Site ) ) )
	{
		return UESoft::eUnKnownObjectPtrType;
	}
	Site* pSite = static_cast<Site*>( pObj );

	DatabaseBase* pBase = NULL;
	pBase = GetDatabase();
	ASSERT( pBase != NULL );
	DatabaseElement* pDbE = NULL;
	pDbE = pBase->GetDatabaseElement( _T("SiteData") );
	ASSERT( pDbE != NULL );

	SiteTable* pST = static_cast<SiteTable*>( pDbE );
	es = pST->Open();
	ASSERT( es == UESoft::eOk || es == UESoft::ePersisterFacadeIsOpen );

	pST->NewRecord();
	pST->SetID( pSite->Id().ObjectID() );
	pST->SetParentID( pSite->OwnerId().ObjectID() );
	pST->SetDescription( pSite->Description() );
	pST->SetPurpose( pSite->GetPurpose() );
	pST->SetFunction( pSite->GetFunction() );
	pST->SetNumber( pSite->GetNumber() );
	pST->SetArea( pSite->GetArea() );
	pST->SetModule( pSite->GetModule() );
	pST->SetRlstored( pSite->GetRlstored() );

	return UESoft::eOk;
}

UESoft::ErrorStatus SitePersist::Update( Persistable* pObj )
{
	if ( NULL == pObj )
	{
		return UESoft::eNullEntityPointer;
	}
	if ( !pObj->IsKindOf( UE_RUNTIME_CLASS( Site ) ) )
	{
		return UESoft::eUnKnownObjectPtrType;
	}
	Site* pSite = static_cast<Site*>( pObj );

	DatabaseBase* pBase = NULL;
	pBase = GetDatabase();
	ASSERT( pBase != NULL );
	DatabaseElement* pDbE = NULL;
	pDbE = pBase->GetDatabaseElement( _T("SiteData") );
	ASSERT( pDbE != NULL );

	SiteTable* pST = static_cast<SiteTable*>( pDbE );
	UESoft::ErrorStatus es = pST->Open();
	ASSERT( es == UESoft::eOk || es == UESoft::ePersisterFacadeIsOpen );

	pST->UpdateRecord( pSite->Id().ObjectID() );
	pST->SetParentID( pSite->OwnerId().ObjectID() );
	pST->SetDescription( pSite->Description() );
	pST->SetPurpose( pSite->GetPurpose() );
	pST->SetFunction( pSite->GetFunction() );
	pST->SetNumber( pSite->GetNumber() );
	pST->SetArea( pSite->GetArea() );
	pST->SetModule( pSite->GetModule() );
	pST->SetRlstored( pSite->GetRlstored() );

	return UESoft::eOk;
}